package flc.ast.activity;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ImageUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.utils.Matrix3;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CropAdapter;
import flc.ast.bean.MyCropBean;
import flc.ast.databinding.ActivityChangeSizeBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class ChangeSizeActivity extends BaseAc<ActivityChangeSizeBinding> {

    public static String imgPath = "";
    private Bitmap mImgBitmap;
    private CropAdapter cropAdapter;
    private int oldPosition = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_change_size;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivChangeSizeBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvStartCut.setOnClickListener(this);

        mDataBinding.rvCropList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        cropAdapter = new CropAdapter();
        mDataBinding.rvCropList.setAdapter(cropAdapter);
        cropAdapter.setOnItemClickListener(this);

    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvStartCut://开始修剪
                saveImg();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        cropAdapter.getItem(oldPosition).setSelect(false);
        cropAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        cropAdapter.getItem(position).setSelect(true);
        cropAdapter.notifyItemChanged(position);
        MyCropBean item = (MyCropBean) adapter.getItem(position);
        mDataBinding.cropImgView.setRatioCropRect(mDataBinding.imageViewTouch.getBitmapRect(), item.getRatio());
    }

    @Override
    protected void initData() {
        if (TextUtils.isEmpty(imgPath)) return;
        mImgBitmap = ImageUtils.getBitmap(imgPath);
        mDataBinding.imageViewTouch.setImageBitmap(mImgBitmap);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放
        mDataBinding.imageViewTouch.post(new Runnable() {
            @Override
            public void run() {
                RectF bitmapRect = mDataBinding.imageViewTouch.getBitmapRect();
                mDataBinding.cropImgView.setRatioCropRect(bitmapRect, -1f);
            }
        });
        //常用尺寸数据
        List<MyCropBean> list = new ArrayList<>();
        list.add(new MyCropBean(getString(R.string.free_crop), getString(R.string.custom_size), 1, 1, -1f, true));
        list.add(new MyCropBean(getString(R.string.crop_kind1), "25x35mm 295x413px", 295, 413, 2 / 3f, false));
        list.add(new MyCropBean(getString(R.string.crop_kind2), "35x49mm 413x579px", 413, 579, 3 / 4f, false));
        list.add(new MyCropBean(getString(R.string.crop_kind3), "22x32mm 260x378px", 260, 378, 3 / 5f, false));
        list.add(new MyCropBean(getString(R.string.crop_kind4), "35x45mm 413x531px", 413, 531, 4 / 5f, false));
        list.add(new MyCropBean(getString(R.string.crop_kind5), "35x53mm 413x626px", 413, 626, 4 / 6f, false));
        list.add(new MyCropBean(getString(R.string.crop_kind6), "55x84mm 649x991px", 649, 991, 2 / 4f, false));
        list.add(new MyCropBean(getString(R.string.crop_kind7), "76x102mm 898x1205px", 898, 1205, 5 / 6f, false));
        list.add(new MyCropBean(getString(R.string.crop_kind8), "89x127mm 1050x1499px", 1050, 1499, 6 / 8f, false));
        cropAdapter.setList(list);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.saving));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                RectF cropRect = mDataBinding.cropImgView.getCropRect();// 剪切区域矩形
                Matrix touchMatrix = mDataBinding.imageViewTouch.getImageViewMatrix();
                float[] data = new float[9];
                touchMatrix.getValues(data);// 底部图片变化记录矩阵原始数据
                Matrix3 cal = new Matrix3(data);// 辅助矩阵计算类
                Matrix3 inverseMatrix = cal.inverseMatrix();// 计算逆矩阵
                Matrix m = new Matrix();
                m.setValues(inverseMatrix.getValues());
                m.mapRect(cropRect);// 变化剪切矩形

                Bitmap resultBit = Bitmap.createBitmap(mImgBitmap,
                        (int) cropRect.left, (int) cropRect.top,
                        (int) cropRect.width(), (int) cropRect.height());
                //保存到相册
                ImageUtils.save2Album(resultBit, Bitmap.CompressFormat.PNG);
                //保存到最近文件
                String filePath = FileUtil.generateFilePath(Content.myCreate, getString(R.string.unit_png));
                ImageUtils.save(resultBit, filePath, Bitmap.CompressFormat.PNG);
                emitter.onNext(filePath);
            }

            @Override
            public void accept(String path) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismissDialog();
                        ChangeSizeResultActivity.imgPath = path;
                        startActivity(ChangeSizeResultActivity.class);
                    }
                }, 500);
            }
        });
    }

}